$monospace: "Source Code Pro", monospace;
$transparent: rgba(0, 0, 0, 0);

:root {
  --aq-bg: white;
  --aq-fg: black;

  // Colors for permission stacks
  // NOTE: if you change these, please update
  // the corresponding rgb values below!
  --aq-read-color: #ff9500;
  --aq-write-color: #0c7bdc;
  --aq-own-color: #dc0055;
  --aq-flow-color: #5ed100;

  --aq-read-rgb-color: 255, 149, 0;
  --aq-write-rgb-color: 12, 123, 220;
  --aq-own-rgb-color: 220, 0, 85;

  // For step table permission colors
  --aq-perm-gain-color: #333;
  --aq-perm-loss-color: #666;

  --aq-error: red;
  --aq-error-text: white;

  --aq-button-border: gray;
  --aq-button-text: gray;
  --aq-button-background: lightgray;
  --aq-button-border-hover: black;
  --aq-button-text-hover: black;
  --aq-button-background-hover: white;

  --aq-step-widget-text: #ccc;
  --aq-step-table-text: black;
  --aq-step-table-border: gainsboro;
  --aq-step-table-background: whitesmoke;
  --aq-step-table-background-alternate: transparent;
}

$base-z-index: 10;
$glyph-width: 14px;

@mixin button {
  background-color: var(--aq-button-background);
  color: var(--aq-button-text);
  opacity: 0;
  font-size: 14px;
  border: 1px solid var(--aq-button-border);
  border-radius: 4px;
  padding: 3px 5px;
  margin: 0px 5px;
  transition: 100ms;
  transition-property: color, border-color, background-color, opacity;
  cursor: pointer;

  &:hover {
    background-color: var(--aq-button-background-hover);
    color: var(--aq-button-text-hover);
    border-color: var(--aq-button-border-hover);
  }
}

.perm {
  font-family: "Verdana";
  font-weight: bold;

  &.missing {
    color: transparent !important;
    -webkit-text-stroke-width: 1px;
  }

  &.read {
    color: var(--aq-read-color);
    -webkit-text-stroke-color: var(--aq-read-color);
  }

  &.write {
    color: var(--aq-write-color);
    -webkit-text-stroke-color: var(--aq-write-color);
  }

  &.own {
    color: var(--aq-own-color);
    -webkit-text-stroke-color: var(--aq-own-color);
  }

  &.flow {
    color: var(--aq-flow-color);
    -webkit-text-stroke-color: var(--aq-flow-color);
  }
}

/* Permission Boundaries */

$perm-animation-duration: 0.5s;

.permission-stack {
  display: inline-flex;
  flex-direction: column;
  position: relative;
  margin-left: 2px;
  margin-right: 2px;

  .animating {
    // Filter courtesy of: https://codepen.io/alvarotrigo/pen/eYEqPZa
    filter: url('data:image/svg+xml,\
      <svg xmlns="http://www.w3.org/2000/svg">\
        <filter id="threshold">\
          <feColorMatrix in="SourceGraphic" type="matrix" values="\
            1 0 0 0 0\
            0 1 0 0 0\
            0 0 1 0 0\
            0 0 0 255 -140" />\
        </filter>\
      </svg>#threshold');
  }

  transition: margin-top $perm-animation-duration ease-in-out,
    margin-bottom $perm-animation-duration ease-in-out,
    bottom $perm-animation-duration ease-in-out;

  cursor: default;

  &.before-whitespace {
    margin-left: -3px;
  }

  &.stack-size-1 {
    top: 1px;
  }

  &.stack-size-2 {
    bottom: 4px;
    margin-top: 5px;
  }

  &.stack-size-3 {
    bottom: 8px;
    margin-top: 6px;
  }

  .perm {
    font-size: $glyph-width;
    text-align: right;
    display: grid;
    grid-template-columns: 1fr;

    > div {
      transition: filter $perm-animation-duration ease-in-out,
        opacity $perm-animation-duration ease-in-out,
        width $perm-animation-duration ease-in-out,
        height $perm-animation-duration ease-in-out;

      grid-row-start: 1;
      grid-column-start: 1;
      text-align: center;
    }

    > .small {
      filter: blur(0px);
      width: 10px;
      height: 10px;
    }

    > .big {
      opacity: 0;
      font-weight: bold;
      filter: blur(3px);

      // same as .small to avoid rubber-band visual effect
      width: 10px;
      height: 10px;
    }

    &.missing > .small {
      -webkit-text-stroke-width: 1.5px;
    }

    &.missing > .big {
      -webkit-text-stroke-width: 1px;
    }
  }

  &:not(.expanded):hover,
  &.expanded {
    &.stack-size-2 {
      margin-top: 7px;
      margin-bottom: -3px;
      bottom: 6px;
    }

    &.stack-size-3 {
      margin-top: 7px;
      bottom: 10px;
    }

    .perm {
      > .small {
        opacity: 0;
        filter: blur(3px);
        width: 0;
        height: 0;
      }

      > .big {
        opacity: 1;
        filter: blur(0px);
        height: 14px;
      }

      &.write > .big {
        width: 14px;
      }

      &:not(.write) > .big {
        width: 12px;
      }
    }
  }
}

.perm-diff-sub-container {
  position: relative;
  width: min-content;
  display: inline-block;
}
.perm-diff-sub {
  position: absolute;
  left: calc((100% - $glyph-width) / 2);
  top: calc((100% - $glyph-width) / 2);
  width: $glyph-width;
  height: $glyph-width;
  background: linear-gradient(
    to top left,
    rgba(0, 0, 0, 0) 0%,
    rgba(0, 0, 0, 0) calc(50% - 1px),
    var(--aq-perm-loss-color) calc(50% - 0.75px),
    var(--aq-perm-loss-color) calc(50% + 0.75px),
    rgba(0, 0, 0, 0) calc(50% + 1px),
    rgba(0, 0, 0, 0) 100%
  );
}
.perm-diff-add {
  color: var(--aq-perm-gain-color);
  margin-right: -2px;
  position: relative;
  bottom: 1px;
  font-size: 80%;
}

.aquascope {
  position: relative;
  height: 100%;
  width: 100%;

  .aquascope-stderr {
    padding: 15px;
    background-color: #f9ffff;
    font-family: $monospace;
    font-size: 0.9em;
    overflow: scroll;
  }

  span.cm-region-end {
    font-family: "Verdana", sans-serif, bold;
    text-align: center;
    vertical-align: bottom;
    padding: 0;
    display: inline-block;
    width: 0;
    overflow: hidden;
    transition: 1s background-color;
  }

  .top-right {
    z-index: $base-z-index;
    position: absolute;
    top: 8px;
    right: 2px;
  }

  .cm-button {
    @include button;
  }

  &:hover .cm-button {
    opacity: 1;
  }

  cm-tooltip-cursor {
    font-size: 0.8em;
    background-color: whitesmoke;
    border-color: gainsboro;
    border: 2px;
    padding: 2px 7px 2px 2px;
    border-radius: 4px;

    &.cm-tooltip-arrow ::before {
      border-top-color: whitesmoke;
    }
    & .cm-tooltip-arrow ::after {
      border-top-color: transparent;
    }
  }

  .cm-scroller {
    overflow: auto;
    // TODO: why was this rule originally here?
    // It causes user selections to not appear in the codemirror editor.
    // z-index: inherit;
  }

  .cm-line.hidden-line {
    display: none;
  }

  .show-hidden .cm-line.hidden-line {
    display: block;
    opacity: 0.6;
  }

  /* Loan Highlighting */

  .aquascope-loan {
    text-decoration: underline 3px $transparent;
    transition: 1s text-decoration;

    &.show-hidden {
      &.read {
        text-decoration: underline 3px var(--aq-read-color) !important;
      }

      &.write {
        text-decoration: underline 3px var(--aq-write-color) !important;
      }

      &.own {
        text-decoration: underline 3px var(--aq-own-color) !important;
      }
    }
  }

  .aquascope-live-region {
    background-color: $transparent;
    border-radius: 5px;
    transition: 1s background-color;

    &.show-hidden {
      &.read {
        background-color: rgba(var(--aq-read-rgb-color), 0.4) !important;
      }

      &.write {
        background-color: rgba(var(--aq-write-rgb-color), 0.4) !important;
      }

      &.own {
        background-color: rgba(var(--aq-own-rgb-color), 0.4) !important;
      }
    }
  }

  /* Permission Steps */
  .hidden-width {
    display: none !important;
  }

  .hidden-height {
    height: 0px;
  }

  .step-widget-toggle {
    cursor: pointer;

    &:hover {
      color: var(--aq-fg);
    }
  }

  .perm-step-widget {
    display: inline-block;
    color: var(--aq-step-widget-text);
    position: relative;
    top: 0.75em;
    margin-bottom: 0.75em;

    @media (max-width: 800px) {
      & {
        display: block;
        top: 0;
        margin-bottom: -0.5em;
        margin-left: 10px;

        .step-widget-spacer {
          max-width: 20px;
        }
      }
    }
  }

  .step-widget-container {
    overflow: hidden;
    display: inline-flex;
    justify-content: start;
  }

  .step-table-dropdown {
    font-size: 0.7em;
    max-height: 15px;
    text-align: center;
    width: 100%;
    cursor: pointer;
    color: var(--aq-step-widget-text);
  }

  .step-table-container {
    overflow: hidden;
    flex-direction: column;
    align-items: flex-start;
    display: inline-flex;
    margin-bottom: 5px;

    background-color: var(--aq-step-table-background);
    border: 2px solid var(--aq-step-table-border);
    border-radius: 4px;

    // So the table appears above the Ferris icon
    position: relative;
    z-index: 100;
  }

  .perm-step-table,
  .step-table-container {
    color: var(--aq-step-table-text);

    td {
      padding: 1px 4px;
    }

    td.perm-step-event {
      text-align: center;
    }
  }

  table.perm-step-table {
    vertical-align: top;
    border-collapse: collapse;

    tr:nth-child(2n) {
      background-color: var(--aq-step-table-background-alternate);
    }
  }

  .perm-step-table tr td {
    border: 1px dotted;
    border-color: var(--aq-step-table-border);
  }

  .contains-hidden .perm-step-table tr {
    td:first-child {
      border-left: none;
    }

    td:last-child {
      border-right: none;
    }
  }

  .perm-char {
    position: relative;
    font: bold $glyph-width "Verdana";
    text-align: center;
    cursor: default;

    // TODO: fix the conflicts w/ other rules
    border-left: none !important;
    border-right: none !important;
    padding: 1px 2px !important;
  }

  .aquascope-action-indicator {
    font-size: 0.9em;
  }
}

.step-marker {
  display: inline-block;
  border-radius: 4px;
  padding: 0 2px;
  background: var(--aq-bg);
  border: 1px solid var(--aq-fg);

  .cm-editor & {
    margin: 2px 6px 0;
  }

  > span {
    font-size: 0.8em;
    color: var(--aq-fg);
    vertical-align: middle;
    position: relative;
    bottom: 1px;
    font-weight: bold;
  }

  &.fail {
    background: var(--aq-error);

    > span {
      color: var(--aq-error-text);
    }
  }
}

.interpreter {
  background: var(--aq-bg);
  padding: 1rem;
  display: flex;
  column-gap: 50px;
  row-gap: 30px;
  flex-wrap: wrap;
  overflow-x: auto;
  position: relative;
  font-size: 15px;

  .concrete-types {
    position: absolute;
    top: 1rem;
    right: 1rem;

    @include button;

    &.active {
      border-color: var(--aq-fg);
      background-color: var(--aq-bg);
      color: var(--aq-fg);
    }
  }

  .step {
    .header {
      position: relative;

      .header-text {
        position: relative;
        z-index: $base-z-index + 2;
      }

      .header-bg {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: $base-z-index + 1;
        background: var(--aq-bg);
        opacity: 0.8;
      }
    }

    .step-header {
      display: flex;
      align-items: flex-start;
      gap: 10px;
      margin-bottom: 5px;

      .step-marker {
        font-size: 1.1em;
        font-family: $monospace;
      }

      .undefined-behavior {
        color: var(--aq-error);
        max-width: 250px;
      }
    }

    .memory-header {
      margin-bottom: 5px;
      font-weight: bold;
    }

    .memory-container {
      position: relative;

      .memory-container-flex {
        display: flex;
        flex-direction: row;
        gap: 60px;
        padding-left: 10px;
      }

      .memory {
        border: 1px dashed var(--aq-step-table-border);
        padding: 0.7rem;
        height: max-content;

        .frames {
          display: flex;
          flex-direction: column;
          gap: 10px;

          .frame-header {
            display: inline-block;
            font-family: $monospace;
          }

          .empty-frame {
            font-size: 0.9em;
          }
        }
      }
    }

    h2 {
      margin-top: 0;
      font-size: 1.1em;
      font-weight: bold;
    }

    code {
      font-size: 0.9em;
    }

    table.locals,
    .heap > table {
      border: 2px solid var(--aq-step-table-border);

      tr.moved {
        opacity: 0.3;
        
        background: repeating-linear-gradient(
          45deg, 
          var(--aq-step-table-border), 
          var(--aq-step-table-border) 3px, 
          var(--aq-bg) 3px, 
          var(--aq-bg) 10px)
      }

      > tbody > tr > td {
        border-width: 2px;
        padding: 2px 4px;

        &:nth-child(1) {
          border-right-width: 1.5px;
        }

        &:nth-child(2) {
          border-left-width: 1.5px;
        }
      }
    }

    table {
      border: 1px solid var(--aq-step-table-border);
      border-collapse: collapse;
      font-family: $monospace;

      &.array {
        border: none;

        td {
          border-top: none;
          border-bottom: none;
          padding: 0 2px;

          &:first-child {
            border-left: none;
          }

          &:last-child {
            border-right: none;
          }
        }
      }

      td {
        border: 1px solid var(--aq-step-table-border);
        transition: width 1s, height 1s;
      }
    }

    .unallocated {
      &.error {
        display: inline-block;
        width: 1.2em; // should be same as line-height

        color: var(--aq-error);
        border: 1px dashed var(--aq-error);
        border-radius: 50%;
        text-align: center;
      }
    }
  }
}

.leader-line {
  z-index: $base-z-index;
}
